package com.mm.serivce.taxi.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.domain.taxi.DMiniUserInfo;
import com.mm.mapper.taxi.DMiniUserInfoMapper;
import com.mm.serivce.taxi.IDMiniUserInfoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 小程序用户信息表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-13
 */
@Service
public class DMiniUserInfoServiceImpl extends ServiceImpl<DMiniUserInfoMapper, DMiniUserInfo> implements IDMiniUserInfoService {

    private final DMiniUserInfoMapper miniUserInfoMapper;

    public DMiniUserInfoServiceImpl(DMiniUserInfoMapper miniUserInfoMapper) {
        this.miniUserInfoMapper = miniUserInfoMapper;
    }

    @Override
    public Page<DMiniUserInfo> queryMiniUserPage(Integer page, Integer limit, int type, String mName) {
        Page<DMiniUserInfo> miniUserIPage = new Page<>(page, limit);
        LambdaQueryWrapper<DMiniUserInfo> queryWrapper = new LambdaQueryWrapper<DMiniUserInfo>();
        if (StrUtil.isNotBlank(mName)) {
            queryWrapper.like(DMiniUserInfo::getMName, mName);
        }
        queryWrapper.eq(DMiniUserInfo::getMType, type);
        queryWrapper.eq(DMiniUserInfo::getDelFlg, '0');
        miniUserIPage = miniUserInfoMapper.selectPage(miniUserIPage, queryWrapper);
        miniUserIPage.setTotal(miniUserIPage.getRecords().size());
        return miniUserIPage;
    }

    @Override
    public List<DMiniUserInfo> queryMiniUserAll(int type) {
        LambdaQueryWrapper<DMiniUserInfo> queryWrapper = new LambdaQueryWrapper<DMiniUserInfo>();
        queryWrapper.eq(DMiniUserInfo::getMType, type);
        queryWrapper.eq(DMiniUserInfo::getDelFlg, '0');
        List<DMiniUserInfo> taxiAddressInfos = miniUserInfoMapper.selectList(queryWrapper);
        return taxiAddressInfos;
    }

    @Override
    public DMiniUserInfo queryMiniUserOne(Integer mId) {
        DMiniUserInfo miniUserInfo = new DMiniUserInfo();
        if (mId > 0){
            miniUserInfo = miniUserInfoMapper.selectById(mId);
        }
        return miniUserInfo;
    }

    @Override
    public DMiniUserInfo queryMiniUserByMap(DMiniUserInfo userInfo) {
        DMiniUserInfo user = new DMiniUserInfo();
        LambdaQueryWrapper<DMiniUserInfo> queryWrapper = new LambdaQueryWrapper<DMiniUserInfo>();
        if (StrUtil.isNotBlank(userInfo.getOpenId())) {
            queryWrapper.eq(DMiniUserInfo::getOpenId, userInfo.getOpenId());
            queryWrapper.eq(DMiniUserInfo::getDelFlg, '0');
            user = miniUserInfoMapper.selectOne(queryWrapper);
        }
        return user;
    }

    @Override
    public boolean updateMiniUser(DMiniUserInfo userInfo) {
        boolean flag = false;
        LambdaQueryWrapper<DMiniUserInfo> queryWrapper = new LambdaQueryWrapper<DMiniUserInfo>();
        // 验证openId 用户ID
        if (userInfo.getMId() != null && userInfo.getMId() > 0) {
            queryWrapper.eq(DMiniUserInfo::getMId, userInfo.getMId());
        }
        if (StrUtil.isNotBlank(userInfo.getOpenId())) {
            queryWrapper.eq(DMiniUserInfo::getOpenId, userInfo.getOpenId());
        }
        /**
         * isEmptyOfEntity 深层实体判断属性
         * isEmptyOfNormal 查询条件为空(不包含entity)
         * isEmptyOfWhere 查询条件为空(包含entity)
         */
        if (queryWrapper.nonEmptyOfNormal()) {
            int result = miniUserInfoMapper.update(userInfo, queryWrapper);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean removeMiniUser(Integer mId) {
        boolean flag = false;
        DMiniUserInfo miniUserInfo = miniUserInfoMapper.selectById(mId);
        if (Objects.nonNull(miniUserInfo)) {
            miniUserInfo.setDelFlg(1);
            Integer result = miniUserInfoMapper.updateById(miniUserInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
